<?php

	include_once('../sys/sys.php');
	include_once('jsss.php');
	header('Content-Type: text/html; charset=utf-8');
	
	$sql = '';
	//获取系统内容以及分页
	
	//获取分类以及分页号
	$st = $_GET['st'];
	$cid = $_GET['cid'];
	$jid = $_GET['jid'];
	if(is_null($st)){$st = 0;}
	//列出的图片数量(注意,不要超过15张)
	$listnum = 1;
	$st=0;
	
	$array_piclist = array();
	
	
	$DB->build_query(array(
			'select' 	=> 'id,name,lastupdate,count',
			'from'		=> '89890tags',
			'order'		=> 'lastupdate DESC',
			'limit'		=> array($st,$listnum)
	));
	$qid = $DB->exec_query();
	$i = 0;
	
	while ($row = $DB->fetch_row($qid)) {
		if($row['lastupdate'] <= 0) $row['lastupdate'] = 0;
		$row['name'] = $row['name'];
		$array_piclist[$i]['tagname'] = $row['name'];
		$array_piclist[$i]['id'] = $row['id'];
		$array_piclist[$i]['count'] = $row['count'];
		$array_piclist[$i]['lastupdate'] = get_date_time($row['lastupdate']);
		$array_piclist[$i]['downloadpicslink'] = SITE_URL . '89890/pics.php?action=getpicfiles&tid=' .$row['id'];
		$array_piclist[$i]['picslink'] = SITE_URL . '89890/pics.php?action=getpicurls&tid=' .$row['id'];
		$array_piclist[$i]['getnewlink'] = SITE_URL . '89890/cr.php?tid=' .$row['id'];
		$i++;
		//这里插入漫画名称
		
		$sql .= mk_book_ist($row);
		//echo mk_book_ist($row);//exit;
		//echo "\r\n";echo "<br>";
		//获取该ID下的章节
		$DB->build_query(array(
			'select' 	=> '*',
			'from'		=> '89890photos',
			'where'		=>	'uid=' . $row['id'],
		));
		$tid = $DB->exec_query();
		$j = 0;
		//设置ID初始值（可以考虑从库中取出最大值+1后作为当前值）
		
		
		$init_num = 1;
		
		
		
		while ($rowc = $DB->fetch_row($tid)) {
			$j++;
			//这里插入章节名称
			$sql .= is_cpt($row['id'], $rowc, $row['name'], '第'.$j.'节' ,$j);
			//echo is_cpt($row['id'], $rowc, $row['name'], '第'.$j.'节' ,$j);
			//echo "\r\n";echo "<br>";
			//获取该章节下的图片
			$nums = $rowc['count'];
			$pre_url = $rowc['url'];
			//这里插入具体内容
			switch ($rowc['vol']) {
				case 2:
					$k=0;
					$pics = dlvss(vol2($nums,$rowc['tpf2'],$pre_url,$volpic888));
					foreach ($pics as $v) {
						$k++;
						$sql .= is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo "\r\n";echo "<br>";
					}
				break;
				case 3:
					$k=0;
					$pics = dlvss(vol3($nums,$rowc['tpf2'],$pre_url,$volpic888));
					foreach ($pics as $v) {
						$k++;
						$sql .= is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo "\r\n";echo "<br>";
					}
				break;
				case 4:
					$k=0;
					$pics = dlvss(vol4($nums,$rowc['tpf2'],$pre_url,$volpic888));
					foreach ($pics as $v) {
						$k++;
						$sql .= is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo "\r\n";echo "<br>";
					}
				break;
				case 5:
					$k=0;
					$pics = dlvss(vol5($nums,$rowc['tpf2'],$pre_url,$volpic888));
					foreach ($pics as $v) {
						$k++;
						$sql .= is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo "\r\n";echo "<br>";
					}
				break;
				case 6:
					$k=0;
					$pics = dlvss(vol6($nums,$rowc['tpf2'],$pre_url,$volpic888));
					foreach ($pics as $v) {
						$k++;
						$sql .= is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo "\r\n";echo "<br>";
					}
				break;
				case 7:
					$k=0;
					$pics = dlvss(vol7($nums,$rowc['tpf2'],$pre_url,$volpic888));
					foreach ($pics as $v) {
						$k++;
						$sql .= is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo "\r\n";echo "<br>";
					}
				break;
				case 8:
					$k=0;
					$pics = dlvss(vol8($nums,$rowc['tpf2'],$pre_url,$volpic888));
					foreach ($pics as $v) {
						$k++;
						$sql .= is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo "\r\n";echo "<br>";
					}
				break;
				case 9:
					$k=0;
					$pics = dlvss(vol9($nums,$rowc['tpf2'],$pre_url,$volpic888));
					foreach ($pics as $v) {
						$k++;
						$sql .= is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo "\r\n";echo "<br>";
					}
				break;
				case 10:
					$k=0;
					$pics = dlvss(vol10($nums,$rowc['tpf2'],$pre_url,$volpic888));
					foreach ($pics as $v) {
						$k++;
						$sql .= is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo is_photo($row['lastupdate'], "", $row['name'], $init_num, 1, $row['id'], $k, $v);
						//echo "\r\n";echo "<br>";
					}
				break;
				default:
				$pics = '数据错误' . "\r\n";
				break;
			}
			$init_num++;
			}
	}
	file_put_contents('1.sql', $sql);
	echo '完结';
	//print_r($array_piclist);
	exit;
	
	
	
	
	
	
	//用于生成漫画的
	function mk_book_ist($row){
		$inQuery = "INSERT INTO `dede_story_books`(
		`bid`,
		`catid`,
		`bcatid`,
		`ischeck`,
		`booktype`,
		`click`,
		`bookname`,
		`author`,
		`mid`,
		`litpic`,
		`pubdate`,
		`status`,
		`lastpost`,
		`postnum`,
		`lastfeedback`,
		`feedbacknum`,
		`weekcc`,
		`monthcc`,
		`weekup`,
		`monthup`,
		`description`,
		`body`,
		`keywords`,
		`userip`,
		`senddate` )
		VALUES (" . 
		$row['id'] . ",
		1,
		0,
		1,
		1,
		'0','" . 
		$row['name'] . "',
		'漫画作者',
		0,
		'/test/uploads/101230/1-101230142532L5.jpg'," . 
		$row['lastupdate'] . ",
		'0', 
		'0'," .
		$row['count'] . ",
		'0', 
		'0', 
		'0', 
		'0', 
		'0', 
		'0',
		'描述信息' , 
		'简介内容' , 
		'关键字', 
		'127.0.0.1'," . 
		$row['lastupdate'] . ");";
		
		return $inQuery;
	}
	
	//用于为某漫画插入某章
	function is_cpt($bookid,$row,$name,$cname,$j){
		$query = "INSERT INTO `dede_story_chapter`(
		`bookid`,
		`catid`,
		`chapnum`,
		`mid`,
		`chaptername`,
		`bookname`,
		`postnum`
		)VALUES (".
		$bookid . ", 
		1," .  
		$j. ", 
		0, '" . 
		$cname . "','"
		. $name. "'," . 
		$row['count'] . "
		);";
		return $query;
	}
	
	//获取章节内图片
	function is_photo($addtime,$titlen,$bookname,$chapterid,$catid,$bookid,$sortid,$bigpic){
	   $inQuery = "
       INSERT INTO `dede_story_content`(
       `title`,
       `bookname`,
       `chapterid`,
       `catid`,
       `bcatid`,
       `booktype`,
       `bookid`,
       `sortid`,
       `mid`,
       `bigpic`,
       `body`,
       `addtime`
       ) VALUES (
       '$titlen',
       '$bookname', 
       '$chapterid', 
       '$catid',
       '0',
       '1', 
       '$bookid',
       '$sortid', 
       '0', 
       '$bigpic' , 
       '', 
       '$addtime'
       );";
		return $inQuery;
	}
	
	function dlvss($pics){
		$picss = explode("\r\n",$pics);
		return $picss;
	}
	
	
	
	
	
	/***
	 * 以下是没用的
	 */
	
	//处理分页信息
	//获取总TAG数
	$totalnumbers = get_count_of_field('89890tags','');
	$pagelink = $Elsesky->PageLinkR($st,$totalnumbers,$listnum,SITE_URL."89890/index.php?&st=<:st:>");
	
	//处理最常访问内容
	$ccount = 9;
	$array_ccount = array();
	
	$DB->build_query(array(
			'select' 	=> '*',
			'from'		=> '89890tags',
			'order'		=> 'scount DESC',
			'limit'		=> array(0,$ccount)
	));
	$qid = $DB->exec_query();
	$i = 0;
	while ($row = $DB->fetch_row($qid)) {
		$array_ccount[$i]['tidname'] = $row['name'];
		$array_ccount[$i]['ccount'] = $row['scount'];
		$array_ccount[$i]['tidlink'] = SITE_URL . '89890/search.php?name=' . $row['name'];
		$i++;
	}
	
	
	//初始化模版变量
	$page = init_smart_template_obj('89890/list.html',CACHE_DIR,CACHE_DIR,TEMPLATE_DIR);
	$page->assign("piclist",$array_piclist);
	$page->assign("system",$sysinfo);
	$page->assign("pagelink",$pagelink);
	$page->assign("searchpg",SITE_URL . '89890/search.php');
	$page->assign("title","列表");
	$page->assign("tidlink",$array_ccount);
	$page->assign("ccount",$ccount);

	$page->output();
	
?>